using System.Drawing;
using System.Drawing.Imaging;

namespace MimeCloud.GDI.Imaging
{
    public class Helper
    {
        public static int CompareColours(Color c1, Color c2)
        {
            int alphaDiff = c1.A - c2.A;
            float hueDiff = c1.GetHue() - c2.GetHue();
            float saturationDiff = c1.GetSaturation() - c2.GetSaturation();
            float brightnessDiff = c1.GetBrightness() - c2.GetBrightness();
            return (int) (((alphaDiff*360 + hueDiff +
                            saturationDiff)*255 + brightnessDiff)*255);
        }

        public static void ReplaceColor (ref Image img, Color oldColor, Color newColor)
        {
            Graphics g = Graphics.FromImage(img);

            ImageAttributes att = new ImageAttributes();
            ColorMap map = new ColorMap {OldColor = oldColor, NewColor = newColor};

            att.SetRemapTable(new ColorMap[] {map});

            g.DrawImage(img,
                        new Rectangle(0, 0, img.Width, img.Height),
                        0, 0, img.Width, img.Height, GraphicsUnit.Pixel, att);

            att.Dispose();
        }
    }
}
